<?php

class Season {

    var $id;
    var $season;
    var $comments;
    var $winner;
    var $runnerUp;
    var $hasConferences = false;
    var $conferences;
    var $shortName;
    var $inactive;
    var $commissionerApproval;
    var $opponentApproval;
    var $noTeamEdit;
    var $commissionerId;
    var $image;
    var $imageText;
    var $spiralInc;
    var $spiralStart;
    private $ruleSetIds;

    function Season($season_id) {
        $this->conferences = array ();
        $this->id = $season_id;
        $rs = executeQuery("select season, prize, winner, runner_up, short_name, inactive, commissioner_approval, " .
        		"opponent_approval, no_team_edit, commissioner_id, image, imageText, spiral_inc, spiral_start " .
        		"from seasons where season_id=$season_id");
        $row = mysql_fetch_row($rs);
        $i = 0;
        $this->season = stripslashes($row[$i++]);
        $this->comments = stripslashes($row[$i++]);
        $this->winner = $row[$i++];
        $this->runnerUp = $row[$i++];
        $this->shortName = stripslashes($row[$i++]);
        $this->inactive = $row[$i++];
        $this->commissionerApproval = $row[$i++];
        $this->opponentApproval = $row[$i++];
        $this->noTeamEdit = $row[$i++];
        $this->commissionerId = $row[$i++];
        $this->image = $row[$i++];
        $this->imageText = $row[$i++];
        $this->spiralInc = $row[$i++];
        $this->spiralStart = $row[$i++];
        $rs = executeQuery("select distinct conference from seasons_teams where fk_season_id=$season_id and " .
        		"conference is not null");
        while ($row = mysql_fetch_row($rs)) {
            $this->addConference($row[0]);
        }
        $this->ruleSetIds = array();
        $rs = executeQuery("select srs.fk_rule_set_id from seasons_rule_sets srs where srs.fk_season_id=$season_id");
        while ($row = mysql_fetch_row($rs)) {
        	array_push($this->ruleSetIds, $row[0]);
        }
    }
    
    /**
     * Returns an array containing the IDs of the rule sets allowed in for this season. 
     * NOTE that if ANY season is allowed the array will be empty. 
     */
    function getAllowedRuleSetIds() {
        return $this->ruleSetIds;
    }
    
    /**
     * returns true if the season is restricted to one or more rule sets
     */
    function restrictedRuleSets() {
        return count($this->ruleSetIds) > 0;
    }

    function addConference($conference) {
        array_push($this->conferences, $conference);
        $this->hasConferences = true;
    }

    /**
     * Gets the link to the winners team
     */
    function getWinnerLink(& $teams) {
        if (isset($this->winner) && $this->winner) {
            $team = $this->getWinner($teams);
            if (isset ($team)) {
                return $team->getRosterLink();
            }
        }
        return "";
    }
    
    function getWinner(& $teams) {
		return $teams->getTeam($this->winner);
    }

    /**
     * Gets a link to the runner ups team
     */
    function getRunnerUpLink(& $teams) {
        if (isset($this->runnerUp) && $this->runnerUp) {
            $team = $teams->getTeam($this->runnerUp);
            if (isset ($team)) {
                return $team->getRosterLink();
            }
        }
        return "";
    }
    
    function finalMatch(&$match) {
        $this->runnerUp = $match->homeRec->teamId;
        $this->winner = $match->awayRec->teamId;
        if ($match->homeRec->score > $match->awayRec->score) {
            $this->runnerUp = $match->awayRec->teamId;
            $this->winner = $match->homeRec->teamId;
        }
		$this->inactive=1;
        $this->update();
    }

    /**
     * Updates this season
     */
    function update() {
    	$name = addslashes($this->season);
    	$shortName = addslashes($this->shortName);
    	$comments = addslashes($this->comments);
    	$image = "NULL";
    	$imageText = "NULL";
    	if (strlen($this->image) > 0) {
    	    $image = "'" . $this->image . "'" ;
    	}
    	if (strlen($this->imageText) > 0) {
	    	$imageText = "'" . addslashes($this->imageText). "'" ;
    	}
        return executeQuery("update seasons set season='$name', prize='$comments', winner='$this->winner', " .
        		"runner_up='$this->runnerUp', short_name='$shortName', inactive='$this->inactive', " .
        		"commissioner_approval='$this->commissionerApproval', opponent_approval='$this->opponentApproval', " .
        		"no_team_edit='$this->noTeamEdit', commissioner_id='$this->commissionerId', image=$image, " .
        		"imageText=$imageText, spiral_inc=$this->spiralInc, spiral_start=$this->spiralStart where season_id=$this->id");
    }

    /**
     * Deletes this season from the database. Needs to clean seasons, seasons teams and all associated matches
     */
    function delete() {
        // seasons
        executeQuery("delete from seasons where season_id=$this->id");

        // seasons teams
        executeQuery("delete from seasons_teams where fk_season_id=$this->id");

		$matchDao = new MatchDao();
		$matches = $matchDao->findSeasonMatches($this);
		foreach($matches->matches as $match) {
		    $matchDao->delete($match);
		}
    }

    /**
     * Assumes the team is already in the season
     */
    function updateConference($team) {
        return executeQuery("update seasons_teams set conference='$team->conference' where fk_season_id=$this->id and fk_team_id=$team->id");
    }
    
    function getImage() {
	    if (strlen($this->image) > 0) {
	    	return "<img src=\"$this->image\" TITLE=\"$this->imageText\" BORDER=\"0\">";
	    } 
	    return ""; 
    }
}
?>